package com.dzl.exchange.service.impl;

import com.dzl.asynthread.SyncInitDataServiceImpl;
import com.dzl.base.service.BaseServiceImpl;
import com.dzl.config.database.Master;
import com.dzl.config.exception.CommonException;
import com.dzl.constant.CommonConstants;
import com.dzl.constant.WelfareGoodsEnum;
import com.dzl.exchange.dao.WelfareExchangeMapper;
import com.dzl.exchange.pojo.WelfareExchange;
import com.dzl.exchange.pojo.WelfareExchangeMeal;
import com.dzl.exchange.pojo.WelfareExchangeMealGoods;
import com.dzl.exchange.pojo.WelfareExchangeSecret;
import com.dzl.exchange.pojo.dto.ExchangeMealGoodsDto;
import com.dzl.exchange.pojo.dto.GoodsSkuDto;
import com.dzl.exchange.pojo.query.ExchangeGroupQuery;
import com.dzl.exchange.pojo.vo.ExchangeGroupVo;
import com.dzl.exchange.pojo.vo.ExchangeMealGoodsVo;
import com.dzl.exchange.pojo.vo.ExchangeSkuGoodsVo;
import com.dzl.exchange.service.*;
import com.dzl.goods.pojo.WelfareGoods;
import com.dzl.goods.pojo.WelfareGoodsAttr;
import com.dzl.goods.pojo.vo.WelfareGoodsSupplierPreviewItemVo;
import com.dzl.goods.pojo.vo.WelfareGoodsSupplierPreviewVo;
import com.dzl.goods.pojo.vo.WelfareSkuGoodsVo;
import com.dzl.goods.service.WelfareGoodsAttrService;
import com.dzl.goods.service.WelfareGoodsService;
import com.dzl.util.DateUtil;
import com.dzl.util.EmptyUtils;
import com.dzl.util.ImageUtils;
import com.dzl.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 兑换券组表(WelfareExchangeSecretGroup)表服务实现类
 * @author caobucao
 * @date 2021-07-09 17:26:09
 */
@Slf4j
@Service
public class WelfareExchangeServiceImpl extends BaseServiceImpl<WelfareExchange> implements WelfareExchangeService {

    @Autowired
    private WelfareExchangeMapper mapper;
    @Autowired
    private WelfareExchangeSecretService welfareExchangeSecretService;
    @Autowired
    private WelfareExchangeGoodsService welfareExchangeGoodsService;
    @Autowired
    private WelfareExchangeMealService welfareExchangeMealService;
    @Autowired
    private WelfareExchangeMealGoodsService welfareExchangeMealGoodsService;
    @Autowired
    private WelfareGoodsService welfareGoodsService;
    @Autowired
    private WelfareGoodsAttrService welfareGoodsAttrService;
    @Autowired
    private SyncInitDataServiceImpl syncInitDataServiceImpl;


    @Override
    public Integer updateExchangeGoodsDisplayPrice(Integer type, Integer exchangeGoodsId, BigDecimal displayPrice){
        if(type == 2){ //选择套餐
            return welfareExchangeMealGoodsService.updateExchangeGoodsDisplayPrice(exchangeGoodsId, displayPrice);
        }
        return welfareExchangeGoodsService.updateExchangeGoodsDisplayPrice(exchangeGoodsId, displayPrice);
    }

    @Override
    public WelfareGoodsSupplierPreviewVo getOrderItemsPreview(Integer type, String itemIds) {
        WelfareGoodsSupplierPreviewVo welfareGoodsSupplierVo = new WelfareGoodsSupplierPreviewVo();
        //商品明細列表
        List<ExchangeSkuGoodsVo> exchangeSkuGoodsVoList = new ArrayList<>();
        if(type == 1){ //选择商品
            exchangeSkuGoodsVoList = welfareExchangeGoodsService.selectExchangeSkuGoodsVoByExchangeGoodsIds(itemIds);
        }else{ //选择套餐
            String[] items = itemIds.split(",");
            for (String str:items) {
                List<ExchangeSkuGoodsVo> exchangeSkuGoodsVoList_ = welfareExchangeMealGoodsService.selectExchangeSkuGoodsVoByMealId(Integer.valueOf(str));
                exchangeSkuGoodsVoList.addAll(exchangeSkuGoodsVoList_);
            }
        }
        //积分总价
        BigDecimal totalCreditsAmount = BigDecimal.ZERO;
        //购物车里面的商品列表
        List<WelfareGoodsSupplierPreviewItemVo> cartGoodsList = new ArrayList<>();
        if(EmptyUtils.isNotEmpty(exchangeSkuGoodsVoList)){
            Map<String, ArrayList<ExchangeSkuGoodsVo>> supplierMap = new HashMap<String, ArrayList<ExchangeSkuGoodsVo>>();
            for (ExchangeSkuGoodsVo exchangeSkuGoods:exchangeSkuGoodsVoList) {
                ArrayList<ExchangeSkuGoodsVo> skuList = supplierMap.get(exchangeSkuGoods.getSupplierCode());
                if(EmptyUtils.isEmpty(skuList)){
                    skuList = new ArrayList<ExchangeSkuGoodsVo>();
                }
                //设置展示价为销售价
                if(EmptyUtils.isNotEmpty(exchangeSkuGoods.getDisplayPrice())){
                    exchangeSkuGoods.setPrice(exchangeSkuGoods.getDisplayPrice());
                }
                //计算积分总价
                totalCreditsAmount = totalCreditsAmount.add(exchangeSkuGoods.getPrice().multiply(new BigDecimal(1)));
                skuList.add(exchangeSkuGoods);
                supplierMap.put(exchangeSkuGoods.getSupplierCode(), skuList);
            }
            if(supplierMap.size()>0) {
                for (Map.Entry<String, ArrayList<ExchangeSkuGoodsVo>> entry : supplierMap.entrySet()) {
                    WelfareGoodsSupplierPreviewItemVo welfareGoodsSupplierListVo = new WelfareGoodsSupplierPreviewItemVo();
                    //供应商维度积分总价
                    BigDecimal supplierCreditsAmount = BigDecimal.ZERO;
                    //供应商下商品数量
                    Integer supplierGoodsSize = 0;
                    welfareGoodsSupplierListVo.setSupplierCode(entry.getKey());
                    List<WelfareSkuGoodsVo> welfareGoodsSupplierGoodsVos = new ArrayList<>();
                    for (ExchangeSkuGoodsVo skuGoodsVo: entry.getValue()) {
                        WelfareSkuGoodsVo welfareGoodsSupplierGoodsVo = new WelfareSkuGoodsVo();
                        BeanUtils.copyProperties(skuGoodsVo,welfareGoodsSupplierGoodsVo);
                        welfareGoodsSupplierGoodsVo.setSkuId(skuGoodsVo.getId());
                        welfareGoodsSupplierGoodsVo.setQuantity(1);
                        if(welfareGoodsSupplierGoodsVos.contains(welfareGoodsSupplierGoodsVo)){
                            Integer oldQuantity = welfareGoodsSupplierGoodsVo.getQuantity();
                            welfareGoodsSupplierGoodsVos.remove(welfareGoodsSupplierGoodsVo);
                            welfareGoodsSupplierGoodsVo.setQuantity(oldQuantity+1);
                            welfareGoodsSupplierGoodsVos.add(welfareGoodsSupplierGoodsVo);
                        }else{
                            welfareGoodsSupplierGoodsVos.add(welfareGoodsSupplierGoodsVo);
                        }
                        supplierCreditsAmount = supplierCreditsAmount.add(skuGoodsVo.getPrice().multiply(new BigDecimal(welfareGoodsSupplierGoodsVo.getQuantity())));
                        supplierGoodsSize = supplierGoodsSize + welfareGoodsSupplierGoodsVo.getQuantity();
                    }
                    welfareGoodsSupplierListVo.setSupplierId(entry.getValue().get(0).getSupplierId());
                    welfareGoodsSupplierListVo.setSupplierName(entry.getValue().get(0).getSupplierName());
                    welfareGoodsSupplierListVo.setSupplierCreditsAmount(supplierCreditsAmount);
                    welfareGoodsSupplierListVo.setSupplierGoodsSize(supplierGoodsSize);
                    welfareGoodsSupplierListVo.setCartGoodsList(welfareGoodsSupplierGoodsVos);
                    cartGoodsList.add(welfareGoodsSupplierListVo);
                }
            }
        }
        welfareGoodsSupplierVo.setTotalCreditsAmount(totalCreditsAmount);
        welfareGoodsSupplierVo.setCartGoodsList(cartGoodsList);
        return welfareGoodsSupplierVo;
    }


    @Override
    public PageInfo<ExchangeGroupVo> selectExchangeGroupVoListPage(ExchangeGroupQuery query) {
        PageHelper.startPage(query);
        List<ExchangeGroupVo> list = mapper.selectExchangeGroupVoListPage(query);
        if(EmptyUtils.isNotEmpty(list)){
            list.forEach(x->{
                Integer countFrozen = x.getCountFrozen();//已冻结
                Integer countIsNotBind = x.getCountIsActivate();//未绑定数 ，实际就等于已激活数，通过数据库查
                Integer countIsUsed = x.getCountIsUsed();//已使用，通过数据库查
                Integer countIsBind = 0;//绑定数
                Integer countIsActive = 0;//已激活
                Integer countIsNotUsed = 0;//未使用
                countIsBind = x.getCountIsBind()+countIsUsed;
                countIsActive = countIsBind+countIsNotBind;
                countIsNotUsed = countIsBind - countIsUsed;
                x.setCountIsActivate(countIsActive);
                x.setCountIsBind(countIsBind);
                x.setCountIsNotBind(countIsNotBind);
                x.setCountIsUsed(countIsUsed);
                x.setCountIsNotUsed(countIsNotUsed);
            });
        }
        PageInfo<ExchangeGroupVo> page = new PageInfo<ExchangeGroupVo>(list);
        return page;
    }

    @Override
    @Master
    @Transactional(rollbackFor = Exception.class)
    public Integer saveExchangeGroup(WelfareExchange group) {
        if(EmptyUtils.isEmpty(group.getId())) {
            //取该批次卡的批次号数据，Q+创建年月日时分秒+6位随机数字
            String batchNumber = StringUtil.getBatchNumber("Q");
            group.setBatchNumber(batchNumber);
            mapper.insertSelective(group);
            //添加完卡之后，生成卡号卡密
            Integer quantity = group.getQuantity();
            List<WelfareExchangeSecret> detailsList = new ArrayList<>();
            //获取当前年份的后两位
            String year = DateUtil.getYear().substring(2);
            //查询当前年份的卡总数
            Integer countYearExchange = welfareExchangeSecretService.selectCountYearExchange("Q"+year);
            IntStream.range(0,quantity).forEach(i -> {
                WelfareExchangeSecret details = new WelfareExchangeSecret();
                details.setGroupId(group.getId());
                //格式： Q21000001 Q21000002
                details.setNumber("Q"+year+String.format("%06d", countYearExchange+i+1));
                details.setSecret("FL"+StringUtil.getRandomStringHaveNumberAndEnglishUpperRemoveIO10(14));
                details.setUpdater(group.getCreater());
                details.setCreater(group.getCreater());
                details.setCreaterName(group.getCreaterName());
                details.setUpdaterName(group.getUpdaterName());
                details.setCreateTime(group.getCreateTime());
                details.setUpdateTime(group.getUpdateTime());
                detailsList.add(details);
            });
            Integer result = welfareExchangeSecretService.insertList(detailsList);
            if(result>0 && EmptyUtils.isNotEmpty(detailsList)){
                if(detailsList.size()==1){
                    syncInitDataServiceImpl.saveWelfareExchangeSecretQrcode(detailsList);
                }else{
                    List<WelfareExchangeSecret> list = new ArrayList<>();
                    for(int i=0;i<detailsList.size();i++){
                        list.add(detailsList.get(i));
                        if(i>0 && (i % 100 == 0 || i == detailsList.size()-1)){
                            log.info("=============================================i:"+i+",listSize:"+list.size());
                            syncInitDataServiceImpl.saveWelfareExchangeSecretQrcode(list);
                            list = new ArrayList<>();
                        }
                    }
                }
            }
            return result;
        } else {
            throw new CommonException("不允许修改！");
        }
    }


    @Override
    public List<ExchangeSkuGoodsVo> getExchangeSkuAndGoods(Integer exchangeId) {
        List<ExchangeSkuGoodsVo> exchangeSkuGoodsVoList = welfareExchangeGoodsService.selectExchangeSkuGoodsVoByExchangeGroupId(exchangeId);
        for (ExchangeSkuGoodsVo exchangeSkuGoodsVo:exchangeSkuGoodsVoList)  {
            if(EmptyUtils.isEmpty(exchangeSkuGoodsVo.getDisplayPrice())){
                exchangeSkuGoodsVo.setDisplayPrice(exchangeSkuGoodsVo.getPrice());
            }
            if(exchangeSkuGoodsVo.getStatus()== CommonConstants.YES && exchangeSkuGoodsVo.getGoodsStatus()== WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getCode()){
                if(exchangeSkuGoodsVo.getIsShow()==CommonConstants.YES){
                    exchangeSkuGoodsVo.setGoodsStatusName(WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getName());
                }else{
                    exchangeSkuGoodsVo.setGoodsStatusName(WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getName());
                }
            }else{
                exchangeSkuGoodsVo.setGoodsStatusName(WelfareGoodsEnum.GOODS_STATUS_TYPE_DOWN.getName());
            }
            StringBuffer sb = new StringBuffer();
            if(EmptyUtils.isNotEmpty(exchangeSkuGoodsVo.getFirstDetailName())){
                sb.append(exchangeSkuGoodsVo.getFirstDetailName());
            }
            if(EmptyUtils.isNotEmpty(exchangeSkuGoodsVo.getSecondDetailName())){
                sb.append(exchangeSkuGoodsVo.getSecondDetailName());
            }
            if(EmptyUtils.isNotEmpty(exchangeSkuGoodsVo.getThirdDetailName())){
                sb.append(exchangeSkuGoodsVo.getThirdDetailName());
            }
            exchangeSkuGoodsVo.setGoodsAttr(sb.toString());
            if (EmptyUtils.isNotEmpty(exchangeSkuGoodsVo.getSkuPic())) {
                exchangeSkuGoodsVo.setGoodsImg(ImageUtils.setImageSrc(exchangeSkuGoodsVo.getSkuPic()));
            }else{
                exchangeSkuGoodsVo.setGoodsImg(ImageUtils.setImageSrc(exchangeSkuGoodsVo.getGoodsImg()));
            }
        };
        return exchangeSkuGoodsVoList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Master
    @Override
    public Map<String, Object> addMealGoodsAndSku(ExchangeMealGoodsDto dto) {
        Map<String, Object> map = new HashMap<>();
        Example mealExample = new Example(WelfareExchangeMeal.class);
        mealExample.createCriteria().andEqualTo("exchangeGroupId",dto.getExchangeGroupId());
        //先删除兑换券套餐商品表数据
            //查出该兑换券的所有套餐
        List<WelfareExchangeMeal> mealList = welfareExchangeMealService.selectByExample(mealExample);
        if(EmptyUtils.isNotEmpty(mealList)) {
            //如果原先有就删除
            //删除该兑换券的所有套餐的所有商品
            List<Integer> mealIds = mealList.stream().map(WelfareExchangeMeal::getId).collect(Collectors.toList());
            //软删除
            welfareExchangeMealGoodsService.deleteByMealIds(mealIds);
            //软删除该兑换券的兑换券套餐表所有数据
            welfareExchangeMealService.deleteByExchangeGroupId(dto.getExchangeGroupId());
        }
        StringBuffer goodsNameBuf = new StringBuffer();
        StringBuffer skuBuf = new StringBuffer();
        StringBuffer skuQuantityBuf = new StringBuffer();
        //再添加兑换券套餐表数据
        dto.getMealList().forEach(x-> {
            WelfareExchangeMeal welfareExchangeMeal = new WelfareExchangeMeal();
            welfareExchangeMeal.setMealName(x.getMealName());
            welfareExchangeMeal.setExchangeGroupId(dto.getExchangeGroupId());
            welfareExchangeMealService.insertSelective(welfareExchangeMeal);
            Integer mealId = welfareExchangeMeal.getId();
            //在循环里面给套餐商品表加数据
            List<GoodsSkuDto> skuGoodsList = x.getSkuGoodsList();
            if(CollectionUtils.isEmpty(skuGoodsList)) {
                throw new CommonException("套餐里面必须有至少一条商品！");
            }
            List<WelfareExchangeMealGoods> mealGoodsList = new ArrayList<>();
            skuGoodsList.forEach(y-> {
                //校验商品是否是上架状态
                Integer goodsId = y.getGoodsId();
                WelfareGoods welfareGoods = welfareGoodsService.selectByPrimaryKey(goodsId);
                if(!welfareGoods.getGoodsStatus().equals(WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getCode())) {
                    String goodsName = welfareGoods.getGoodsName();
                    goodsNameBuf.append(goodsName).append("<nb/>");
                } else {
                    //校验SKU是否是删除状态
                    Integer skuId = y.getSkuId();
                    WelfareGoodsAttr welfareGoodsAttr = welfareGoodsAttrService.selectByPrimaryKey(skuId);
                    if(welfareGoodsAttr.getStatus().equals(0)) {
                        Integer skuGoodsId = welfareGoodsAttr.getGoodsId();
                        WelfareGoods welfareSkuGoods = welfareGoodsService.selectByPrimaryKey(skuGoodsId);
                        skuBuf.append(welfareSkuGoods.getGoodsName()).append("的");
                        if(EmptyUtils.isNotEmpty(welfareGoodsAttr.getFirstDetailName())) {
                            skuBuf.append(welfareGoodsAttr.getFirstDetailName());
                        }
                        if(EmptyUtils.isNotEmpty(welfareGoodsAttr.getSecondDetailName())) {
                            skuBuf.append("-").append(welfareGoodsAttr.getSecondDetailName());
                        }
                        if(EmptyUtils.isNotEmpty(welfareGoodsAttr.getThirdDetailName())) {
                            skuBuf.append("-").append(welfareGoodsAttr.getThirdDetailName());
                        }
                        skuBuf.append("<nb/>");
                    } else {
                        //规格不是删除的才查库存是否充足
                        //检查sku库存是否充足
                        if(welfareGoodsAttr.getQuantity()<=0) {
                            Integer skuGoodsId = welfareGoodsAttr.getGoodsId();
                            WelfareGoods welfareSkuGoods = welfareGoodsService.selectByPrimaryKey(skuGoodsId);
                            skuQuantityBuf.append(welfareSkuGoods.getGoodsName()).append("的");
                            if(EmptyUtils.isNotEmpty(welfareGoodsAttr.getFirstDetailName())) {
                                skuQuantityBuf.append(welfareGoodsAttr.getFirstDetailName());
                            }
                            if(EmptyUtils.isNotEmpty(welfareGoodsAttr.getSecondDetailName())) {
                                skuQuantityBuf.append("-").append(welfareGoodsAttr.getSecondDetailName());
                            }
                            if(EmptyUtils.isNotEmpty(welfareGoodsAttr.getThirdDetailName())) {
                                skuQuantityBuf.append("-").append(welfareGoodsAttr.getThirdDetailName());
                            }
                            skuQuantityBuf.append("<nb/>");
                        }
                    }
                }
                WelfareExchangeMealGoods welfareExchangeMealGoods = new WelfareExchangeMealGoods();
                welfareExchangeMealGoods.setMealId(mealId);
                welfareExchangeMealGoods.setGoodsId(y.getGoodsId());
                welfareExchangeMealGoods.setSkuId(y.getSkuId());
                welfareExchangeMealGoods.setDisplayPrice(y.getDisplayPrice());
                mealGoodsList.add(welfareExchangeMealGoods);
            });
            welfareExchangeMealGoodsService.insertList(mealGoodsList);
        });
        if(goodsNameBuf.length()>0) {
            goodsNameBuf.delete(goodsNameBuf.length()-5, goodsNameBuf.length());
            throw new CommonException(4000, "下列商品已下架，请手动移除后再保存：<br/>"+goodsNameBuf.toString());
        }
        if(skuBuf.length()>0) {
            skuBuf.delete(skuBuf.length()-5, skuBuf.length());
            throw new CommonException(4000, "下列商品规格状态发生变更，请检查后保存：<br/>"+skuBuf.toString());
        }
        if(skuQuantityBuf.length()>0) {
            skuQuantityBuf.delete(skuQuantityBuf.length()-5, skuQuantityBuf.length());
            throw new CommonException(4000, "下列商品规格库存不足，请检查后保存：<br/>"+skuQuantityBuf.toString());
        }
        return map;
    }

    @Override
    public List<ExchangeMealGoodsVo> getExchangeMealCheckedSkuAndGoods(Integer exchangeId) {
        //获取兑换券的所有套餐
        List<ExchangeMealGoodsVo> exchangeMealGoodsVoList = new ArrayList<>();
        Example example = new Example(WelfareExchangeMeal.class);
        example.createCriteria().andEqualTo("exchangeGroupId", exchangeId).andEqualTo("status", 1);
        List<WelfareExchangeMeal> mealList = welfareExchangeMealService.selectByExample(example);
        mealList.forEach(meal -> {
            ExchangeMealGoodsVo exchangeMealGoodsVo = new ExchangeMealGoodsVo();
            BigDecimal totalDisplayPrice = BigDecimal.ZERO;//总展示价
            BigDecimal totalCostPrice = BigDecimal.ZERO;//总成本价
            //通过套餐id查询套餐关联的所有商品
            List<ExchangeSkuGoodsVo> exchangeSkuGoodsVoList = welfareExchangeMealGoodsService.selectExchangeSkuGoodsVoByMealId(meal.getId());
            for (ExchangeSkuGoodsVo exchangeSkuGoodsVo:exchangeSkuGoodsVoList)  {
                if(EmptyUtils.isEmpty(exchangeSkuGoodsVo.getDisplayPrice())){
                    exchangeSkuGoodsVo.setDisplayPrice(exchangeSkuGoodsVo.getPrice());
                }
                if(exchangeSkuGoodsVo.getStatus()== CommonConstants.YES && exchangeSkuGoodsVo.getGoodsStatus()==WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getCode()){
                    if(exchangeSkuGoodsVo.getIsShow()==CommonConstants.YES){
                        exchangeSkuGoodsVo.setGoodsStatusName(WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getName());
                    }else{
                        exchangeSkuGoodsVo.setGoodsStatusName(WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getName());
                    }
                }else{
                    exchangeSkuGoodsVo.setGoodsStatusName(WelfareGoodsEnum.GOODS_STATUS_TYPE_DOWN.getName());
                }
                StringBuffer sb = new StringBuffer();
                if(EmptyUtils.isNotEmpty(exchangeSkuGoodsVo.getFirstDetailName())){
                    sb.append(exchangeSkuGoodsVo.getFirstDetailName());
                }
                if(EmptyUtils.isNotEmpty(exchangeSkuGoodsVo.getSecondDetailName())){
                    sb.append(exchangeSkuGoodsVo.getSecondDetailName());
                }
                if(EmptyUtils.isNotEmpty(exchangeSkuGoodsVo.getThirdDetailName())){
                    sb.append(exchangeSkuGoodsVo.getThirdDetailName());
                }
                exchangeSkuGoodsVo.setGoodsAttr(sb.toString());
                if (EmptyUtils.isNotEmpty(exchangeSkuGoodsVo.getSkuPic())) {
                    exchangeSkuGoodsVo.setGoodsImg(ImageUtils.setImageSrc(exchangeSkuGoodsVo.getSkuPic()));
                }else{
                    exchangeSkuGoodsVo.setGoodsImg(ImageUtils.setImageSrc(exchangeSkuGoodsVo.getGoodsImg()));
                }
                totalDisplayPrice = totalDisplayPrice.add(exchangeSkuGoodsVo.getDisplayPrice());
                totalCostPrice = totalCostPrice.add(exchangeSkuGoodsVo.getCostPrice());
            };
            exchangeMealGoodsVo.setMealId(meal.getId());
            exchangeMealGoodsVo.setMealName(meal.getMealName());
            exchangeMealGoodsVo.setExchangeGroupId(exchangeId);
            exchangeMealGoodsVo.setTotalCostPrice(totalCostPrice);
            exchangeMealGoodsVo.setTotalDisplayPrice(totalDisplayPrice);
            exchangeMealGoodsVo.setExchangeSkuGoodsVoList(exchangeSkuGoodsVoList);
            exchangeMealGoodsVoList.add(exchangeMealGoodsVo);
        });
        return exchangeMealGoodsVoList;
    }

    @Override
    public List<ExchangeSkuGoodsVo> getExchangeMealCheckedSkuAndGoodsByMealId(Integer mealId) {
        //通过套餐id查询套餐关联的所有商品
        List<ExchangeSkuGoodsVo> exchangeSkuGoodsVoList = welfareExchangeMealGoodsService.selectExchangeSkuGoodsVoByMealId(mealId);
        for (ExchangeSkuGoodsVo exchangeSkuGoodsVo:exchangeSkuGoodsVoList)  {
            if(EmptyUtils.isEmpty(exchangeSkuGoodsVo.getDisplayPrice())){
                exchangeSkuGoodsVo.setDisplayPrice(exchangeSkuGoodsVo.getPrice());
            }
            if(exchangeSkuGoodsVo.getStatus()== CommonConstants.YES && exchangeSkuGoodsVo.getGoodsStatus()==WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getCode()){
                if(exchangeSkuGoodsVo.getIsShow()==CommonConstants.YES){
                    exchangeSkuGoodsVo.setGoodsStatusName(WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getName());
                }else{
                    exchangeSkuGoodsVo.setGoodsStatusName(WelfareGoodsEnum.GOODS_STATUS_TYPE_SHELVES.getName());
                }
            }else{
                exchangeSkuGoodsVo.setGoodsStatusName(WelfareGoodsEnum.GOODS_STATUS_TYPE_DOWN.getName());
            }
            StringBuffer sb = new StringBuffer();
            if(EmptyUtils.isNotEmpty(exchangeSkuGoodsVo.getFirstDetailName())){
                sb.append(exchangeSkuGoodsVo.getFirstDetailName());
            }
            if(EmptyUtils.isNotEmpty(exchangeSkuGoodsVo.getSecondDetailName())){
                sb.append(exchangeSkuGoodsVo.getSecondDetailName());
            }
            if(EmptyUtils.isNotEmpty(exchangeSkuGoodsVo.getThirdDetailName())){
                sb.append(exchangeSkuGoodsVo.getThirdDetailName());
            }
            exchangeSkuGoodsVo.setGoodsAttr(sb.toString());
            if (EmptyUtils.isNotEmpty(exchangeSkuGoodsVo.getSkuPic())) {
                exchangeSkuGoodsVo.setGoodsImg(ImageUtils.setImageSrc(exchangeSkuGoodsVo.getSkuPic()));
            }else{
                exchangeSkuGoodsVo.setGoodsImg(ImageUtils.setImageSrc(exchangeSkuGoodsVo.getGoodsImg()));
            }
        };
        return exchangeSkuGoodsVoList;
    }

}
